package com.share.example.sbnfsj.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author syq
 * @data 2019/8/9
 */
@Slf4j
public class ApplicationUtil implements ApplicationContextAware {
    private static ApplicationContext context = null;
    private static String profile = null;

    @Autowired
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    /*获取配置中的值*/
    public static String getEnvironmentProperty(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        } else if (context == null) {
            return null;
        } else {
            return context.getEnvironment().getProperty(key);
        }
    }

    public static  <T> T getBean(String name) {
        if(context == null){
            return null;
        }
        return (T)context.getBean(name);
    }
}
